package com.zj.project.common.base

import android.app.Application
import android.os.Bundle
import androidx.annotation.NonNull
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.zj.project.common.base.livedata.SingleLiveData
import com.zj.project.common.http.HttpClient
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.disposables.Disposable

/**
 * createTime:2021/7/26 15:22
 * auth:张继
 * des:
 */
open class BaseViewModel(@NonNull application: Application) : AndroidViewModel(application),
    ILifecycleObserver {

    val startActivityEvent: SingleLiveData<Map<String, Any>> by lazy {
        SingleLiveData()
    }

    val errorMessage: SingleLiveData<String> by lazy {
        SingleLiveData()
    }


    protected val api by lazy {
        HttpClient.api
    }
    private val compositeDisposable: CompositeDisposable by lazy {
        CompositeDisposable()
    }

    fun addDisposable(disposable: Disposable) {
        compositeDisposable.add(disposable)
    }

    override fun onCreate() {

    }

    override fun onStart() {
    }

    override fun onResume() {
    }

    override fun onPause() {
    }

    override fun onStop() {

    }

    override fun onDestroy() {
        compositeDisposable.dispose()
    }

    fun startActivity(cls: Class<*>) {
        startActivity(cls, null)
    }

    private fun startActivity(cls: Class<*>, bundle: Bundle?) {
        val map: MutableMap<String, Any> = mutableMapOf()
        map["activity"] = cls
        if (bundle != null) {
            map["bundle"] = bundle
        }
        startActivityEvent.value = map
    }


    protected fun showMsg(msg: String) {
        errorMessage.value = msg
    }
}